package com.pioneer.gui.invoice.view;

import javax.swing.UIManager;
import javax.swing.JFrame;
import javax.swing.JPanel;

import java.awt.Dimension;
import java.awt.BorderLayout;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import java.sql.SQLException;

import com.pioneer.gui.invoice.util.ConnectionServer;
import com.pioneer.gui.invoice.model.InvoiceModel;
import com.pioneer.gui.invoice.controller.InvoiceModeltoInvoicePanelAdapter;

public class Invoice
{    
    public Invoice()
    { 
        InvoiceModel invoiceModel=new InvoiceModel();
        // Set the controllers here
        
        
        
        
        try
        {
            UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
            //UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
            
        }
        catch (Exception e)
        {
            System.out.println("Cannot set the look and feel");            
        }       

        //Create the top-level container and add contents to it.
        JFrame frame = new JFrame("Invoice");
        frame.setSize(new Dimension(640,480));
        frame.setResizable(true);
        JPanel invoicePanel = this.createInvoicePanel(invoiceModel);
        frame.getContentPane().add(invoicePanel, BorderLayout.CENTER);

        //Finish setting up the frame, and show it.
        frame.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
            	ConnectionServer connServer=ConnectionServer.getSingleton();
            	try
            	{
            		connServer.getConnection().close();
            		System.out.println("Connection closed.");
            	}
            	catch (SQLException ex)
            	{
            		System.out.println(ex.toString());
            	}
                System.exit(0);
            }
        });
        //frame.pack();
        frame.show();
        //frame.setVisible(true);
    }
	
    public JPanel createInvoicePanel(InvoiceModel invoiceModel) {
    	
        InvoicePanel invoicePanel=new InvoicePanel(invoiceModel);
        invoiceModel.addChangeListener(new InvoiceModeltoInvoicePanelAdapter(invoiceModel,invoicePanel));

        return invoicePanel;
    }

    public static void main(String[] args)
    {
    	System.out.println("Loading data...");
        ConnectionServer connServer=ConnectionServer.getSingleton();
		connServer.initialize(args[0]);
		Invoice invoice = new Invoice();		
    }
}
